﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace WEATHER.Components
{
    public partial class Area
    {
        #region Private members.

        protected int _ID;
        protected Guid _ModuleID = Guid.Empty;
        protected DateTime _Date = new DateTime(1753, 1, 1);
        protected string _AreaName = string.Empty;
        protected string _WeatherSymbol = string.Empty;
        protected string _WeatherDescription = string.Empty;
        protected string _MinimumTemperatureRange = string.Empty;
        protected string _MaximumTemperatureRange = string.Empty;
        protected DateTime _CreatedDate = new DateTime(1753, 1, 1);
        protected string _CreatedBy = string.Empty;

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Properties.

        public int ID
        {
            set { this._ID = value; }
            get { return this._ID; }
        }

        public Guid ModuleID
        {
            set { this._ModuleID = value; }
            get { return this._ModuleID; }
        }

        public DateTime Date
        {
            set { this._Date = value; }
            get { return this._Date; }
        }

        public string AreaName
        {
            set { this._AreaName = value; }
            get { return this._AreaName; }
        }

        public string WeatherSymbol
        {
            set { this._WeatherSymbol = value; }
            get { return this._WeatherSymbol; }
        }

        public string WeatherDescription
        {
            set { this._WeatherDescription = value; }
            get { return this._WeatherDescription; }
        }

        public string MinimumTemperatureRange
        {
            set { this._MinimumTemperatureRange = value; }
            get { return this._MinimumTemperatureRange; }
        }

        public string MaximumTemperatureRange
        {
            set { this._MaximumTemperatureRange = value; }
            get { return this._MaximumTemperatureRange; }
        }

        public DateTime CreatedDate
        {
            set { this._CreatedDate = value; }
            get { return this._CreatedDate; }
        }

        public string CreatedBy
        {
            set { this._CreatedBy = value; }
            get { return this._CreatedBy; }
        }


        #endregion

        //---------------------------------------------------------------------------------------------

        #region Select methods.

        public static Area Load(int id)
        {
            const string spName = "[dbo].[p_WEATHER_Area_Load]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, id);
            Area entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new Area();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetGuid(reader.GetOrdinal("ModuleID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Date"))) entity.Date = reader.GetDateTime(reader.GetOrdinal("Date"));
                if (!reader.IsDBNull(reader.GetOrdinal("AreaName"))) entity.AreaName = reader.GetString(reader.GetOrdinal("AreaName"));
                if (!reader.IsDBNull(reader.GetOrdinal("WeatherSymbol"))) entity.WeatherSymbol = reader.GetString(reader.GetOrdinal("WeatherSymbol"));
                if (!reader.IsDBNull(reader.GetOrdinal("WeatherDescription"))) entity.WeatherDescription = reader.GetString(reader.GetOrdinal("WeatherDescription"));
                if (!reader.IsDBNull(reader.GetOrdinal("MinimumTemperatureRange"))) entity.MinimumTemperatureRange = reader.GetString(reader.GetOrdinal("MinimumTemperatureRange"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaximumTemperatureRange"))) entity.MaximumTemperatureRange = reader.GetString(reader.GetOrdinal("MaximumTemperatureRange"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
            }
            reader.Close();
            return entity;
        }

        //---------------------------------------------------------------------------------------------
        public static List<Area> SelectCollectionAll(Guid moduleID)
        {
            List<Area> collection = new List<Area>();
            SqlDataReader reader = (SqlDataReader)SelectReaderAll(moduleID);
            while (reader.Read())
            {
                Area entity = new Area();

                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetGuid(reader.GetOrdinal("ModuleID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Date"))) entity.Date = reader.GetDateTime(reader.GetOrdinal("Date"));
                if (!reader.IsDBNull(reader.GetOrdinal("AreaName"))) entity.AreaName = reader.GetString(reader.GetOrdinal("AreaName"));
                if (!reader.IsDBNull(reader.GetOrdinal("WeatherSymbol"))) entity.WeatherSymbol = reader.GetString(reader.GetOrdinal("WeatherSymbol"));
                if (!reader.IsDBNull(reader.GetOrdinal("WeatherDescription"))) entity.WeatherDescription = reader.GetString(reader.GetOrdinal("WeatherDescription"));
                if (!reader.IsDBNull(reader.GetOrdinal("MinimumTemperatureRange"))) entity.MinimumTemperatureRange = reader.GetString(reader.GetOrdinal("MinimumTemperatureRange"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaximumTemperatureRange"))) entity.MaximumTemperatureRange = reader.GetString(reader.GetOrdinal("MaximumTemperatureRange"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
                collection.Add(entity);
            }

            reader.Close();
            return collection;
        }

        //---------------------------------------------------------------------------------------------

        public static List<Area> SelectCollectionDynamic(string whereCondition, string orderByExpression)
        {
            List<Area> collection = new List<Area>();

            SqlDataReader reader = (SqlDataReader)SelectReaderDynamic(whereCondition, orderByExpression);
            while (reader.Read())
            {
                Area entity = new Area();

                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetGuid(reader.GetOrdinal("ModuleID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Date"))) entity.Date = reader.GetDateTime(reader.GetOrdinal("Date"));
                if (!reader.IsDBNull(reader.GetOrdinal("AreaName"))) entity.AreaName = reader.GetString(reader.GetOrdinal("AreaName"));
                if (!reader.IsDBNull(reader.GetOrdinal("WeatherSymbol"))) entity.WeatherSymbol = reader.GetString(reader.GetOrdinal("WeatherSymbol"));
                if (!reader.IsDBNull(reader.GetOrdinal("WeatherDescription"))) entity.WeatherDescription = reader.GetString(reader.GetOrdinal("WeatherDescription"));
                if (!reader.IsDBNull(reader.GetOrdinal("MinimumTemperatureRange"))) entity.MinimumTemperatureRange = reader.GetString(reader.GetOrdinal("MinimumTemperatureRange"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaximumTemperatureRange"))) entity.MaximumTemperatureRange = reader.GetString(reader.GetOrdinal("MaximumTemperatureRange"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
                collection.Add(entity);
            }

            reader.Close();
            return collection;
        }

        //---------------------------------------------------------------------------------------------

        // Select by foreign key return collection		


        public static DataSet SelectAll(Guid moduleID)
        {
            const string spName = "[dbo].[p_WEATHER_Area_SelectAll]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
        {
            const string spName = "[dbo].[p_WEATHER_Area_SelectDynamic]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderAll(Guid moduleID)
        {
            const string spName = "[dbo].[p_WEATHER_Area_SelectAll]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
        {
            const string spName = "[dbo].[p_WEATHER_Area_SelectDynamic]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        // Select by foreign key return collection		

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert methods.

        public static int InsertArea(Guid moduleID, DateTime date, string areaName, string weatherSymbol, string weatherDescription, string minimumTemperatureRange, string maximumTemperatureRange, DateTime createdDate, string createdBy)
        {
            Area entity = new Area();
            entity.ModuleID = moduleID;
            entity.Date = date;
            entity.AreaName = areaName;
            entity.WeatherSymbol = weatherSymbol;
            entity.WeatherDescription = weatherDescription;
            entity.MinimumTemperatureRange = minimumTemperatureRange;
            entity.MaximumTemperatureRange = maximumTemperatureRange;
            entity.CreatedDate = createdDate;
            entity.CreatedBy = createdBy;
            return entity.Insert();
        }

        public int Insert()
        {
            return this.Insert(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Insert(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_WEATHER_Area_Insert]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, ModuleID);
            db.AddInParameter(dbCommand, "@Date", SqlDbType.DateTime, Date.Year <= 1753 ? DBNull.Value : (object)Date);
            db.AddInParameter(dbCommand, "@AreaName", SqlDbType.NVarChar, AreaName);
            db.AddInParameter(dbCommand, "@WeatherSymbol", SqlDbType.VarChar, WeatherSymbol);
            db.AddInParameter(dbCommand, "@WeatherDescription", SqlDbType.NVarChar, WeatherDescription);
            db.AddInParameter(dbCommand, "@MinimumTemperatureRange", SqlDbType.NVarChar, MinimumTemperatureRange);
            db.AddInParameter(dbCommand, "@MaximumTemperatureRange", SqlDbType.NVarChar, MaximumTemperatureRange);
            db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, CreatedDate.Year <= 1753 ? DBNull.Value : (object)CreatedDate);
            db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.VarChar, CreatedBy);

            if (transaction != null)
            {
                db.ExecuteNonQuery(dbCommand, transaction);
                ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return ID;
            }
            else
            {
                db.ExecuteNonQuery(dbCommand);
                ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return ID;
            }
        }

        //---------------------------------------------------------------------------------------------
        public static bool InsertCollection(IList<Area> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (Area item in collection)
                        {
                            if (item.Insert(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at InsertCollection method: " + ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert / Update methods.

        public static int InsertUpdateArea(int id, Guid moduleID, DateTime date, string areaName, string weatherSymbol, string weatherDescription, string minimumTemperatureRange, string maximumTemperatureRange, DateTime createdDate, string createdBy)
        {
            Area entity = new Area();
            entity.ID = id;
            entity.ModuleID = moduleID;
            entity.Date = date;
            entity.AreaName = areaName;
            entity.WeatherSymbol = weatherSymbol;
            entity.WeatherDescription = weatherDescription;
            entity.MinimumTemperatureRange = minimumTemperatureRange;
            entity.MaximumTemperatureRange = maximumTemperatureRange;
            entity.CreatedDate = createdDate;
            entity.CreatedBy = createdBy;
            return entity.InsertUpdate();
        }

        //---------------------------------------------------------------------------------------------

        public int InsertUpdate()
        {
            return this.InsertUpdate(null);
        }

        //---------------------------------------------------------------------------------------------

        public int InsertUpdate(SqlTransaction transaction)
        {
            const string spName = "p_WEATHER_Area_InsertUpdate";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, ModuleID);
            db.AddInParameter(dbCommand, "@Date", SqlDbType.DateTime, Date.Year == 1753 ? DBNull.Value : (object)Date);
            db.AddInParameter(dbCommand, "@AreaName", SqlDbType.NVarChar, AreaName);
            db.AddInParameter(dbCommand, "@WeatherSymbol", SqlDbType.VarChar, WeatherSymbol);
            db.AddInParameter(dbCommand, "@WeatherDescription", SqlDbType.NVarChar, WeatherDescription);
            db.AddInParameter(dbCommand, "@MinimumTemperatureRange", SqlDbType.NVarChar, MinimumTemperatureRange);
            db.AddInParameter(dbCommand, "@MaximumTemperatureRange", SqlDbType.NVarChar, MaximumTemperatureRange);
            db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, CreatedDate.Year == 1753 ? DBNull.Value : (object)CreatedDate);
            db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.VarChar, CreatedBy);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------
        public static bool InsertUpdateCollection(IList<Area> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (Area item in collection)
                        {
                            if (item.InsertUpdate(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at InsertUpdateCollection method: " + ex.Message);

                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }
        #endregion

        //---------------------------------------------------------------------------------------------

        #region Update methods.

        public static int UpdateArea(int id, Guid moduleID, DateTime date, string areaName, string weatherSymbol, string weatherDescription, string minimumTemperatureRange, string maximumTemperatureRange, DateTime createdDate, string createdBy)
        {
            Area entity = new Area();
            entity.ID = id;
            entity.ModuleID = moduleID;
            entity.Date = date;
            entity.AreaName = areaName;
            entity.WeatherSymbol = weatherSymbol;
            entity.WeatherDescription = weatherDescription;
            entity.MinimumTemperatureRange = minimumTemperatureRange;
            entity.MaximumTemperatureRange = maximumTemperatureRange;
            entity.CreatedDate = createdDate;
            entity.CreatedBy = createdBy;
            return entity.Update();
        }

        //---------------------------------------------------------------------------------------------

        public int Update()
        {
            return this.Update(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Update(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_WEATHER_Area_Update]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, ModuleID);
            db.AddInParameter(dbCommand, "@Date", SqlDbType.DateTime, Date.Year == 1753 ? DBNull.Value : (object)Date);
            db.AddInParameter(dbCommand, "@AreaName", SqlDbType.NVarChar, AreaName);
            db.AddInParameter(dbCommand, "@WeatherSymbol", SqlDbType.VarChar, WeatherSymbol);
            db.AddInParameter(dbCommand, "@WeatherDescription", SqlDbType.NVarChar, WeatherDescription);
            db.AddInParameter(dbCommand, "@MinimumTemperatureRange", SqlDbType.NVarChar, MinimumTemperatureRange);
            db.AddInParameter(dbCommand, "@MaximumTemperatureRange", SqlDbType.NVarChar, MaximumTemperatureRange);
            db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, CreatedDate.Year == 1753 ? DBNull.Value : (object)CreatedDate);
            db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.VarChar, CreatedBy);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------
        public static bool UpdateCollection(IList<Area> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (Area item in collection)
                        {
                            if (item.Update(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at UpdateCollection method: " + ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Delete methods.

        public static int DeleteArea(int id)
        {
            Area entity = new Area();
            entity.ID = id;

            return entity.Delete();
        }

        public int Delete()
        {
            return this.Delete(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Delete(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_WEATHER_Area_Delete]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static bool DeleteCollection(IList<Area> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (Area item in collection)
                        {
                            if (item.Delete(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at DeleteCollection method: " + ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }
        #endregion
    }
}